

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_woo_2025/common/index.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

class ConfigService extends GetxService {

  //这是一个单例写法
  static ConfigService get to => Get.find();

  //包信息
  PackageInfo? _platform;

  //版本号
  String get version => _platform?.version ?? "";

  //多语言
  Locale locale = PlatformDispatcher.instance.locale;

  //主题
  AdaptiveThemeMode themeMode = AdaptiveThemeMode.light;

  //初始化
  Future<ConfigService> init() async {
    //获取平台信息
    await getPlatform();
    //主题
    await initTheme();

    //多语言初始化
    initLocale();

    return this;
  }

  Future<void> getPlatform() async {
    _platform = await PackageInfo.fromPlatform();
  }

  //初始化主题
  Future<void> initTheme() async {
    final savedThemeMode = await AdaptiveTheme.getThemeMode();
    themeMode = savedThemeMode ?? AdaptiveThemeMode.light;
  }

    // 切换 theme
  Future<void> setThemeMode(String themeKey) async {
    switch (themeKey) {
      case "light":
        AdaptiveTheme.of(Get.context!).setLight();
        break;
      case "dark":
        AdaptiveTheme.of(Get.context!).setDark();
        break;
      case "system":
        AdaptiveTheme.of(Get.context!).setSystem();
        break;
    }
  }


  //初始化语言
  void initLocale() {
    var langCode = Storage().getString(Constants.storageLanguageCode);
    if (langCode.isEmpty) return;
    var index = Translation.supportedLocales.indexWhere((element) => element.languageCode == langCode);
    if (index < 0 ) return;
    locale = Translation.supportedLocales[index];
  }

  //更改语言
  void setLanguage(Locale value ){
    locale = value;
    Storage().setString(Constants.storageLanguageCode, locale.languageCode);
    Get.updateLocale(locale);
  }
}